Namespace persistent

    Public Class MassiveTariffChangeProcessor
        Public Delegate Sub MassiveTariffChageRequestHandler(ByVal filePath As String, ByVal context As System.Web.HttpContext)
        Public Event MassiveTariffChageRequest As MassiveTariffChageRequestHandler

        Public Sub ProcessRequest(ByVal request As MassiveTariffChangeRequest)

            Dim srControlTemplate As New IO.StreamReader(request.LocalWorkDirectory & "/control.template")
            Dim srInputFile As New IO.StreamReader(request.InputStream)

            Dim sqlLoaderControlTemplate As String = srControlTemplate.ReadToEnd()
            Dim sqlLoaderControlQuery As String = sqlLoaderControlTemplate.Replace("[REQUESTID]", Me.getNextRequestID().ToString())
            sqlLoaderControlQuery = sqlLoaderControlQuery.Replace("[REQUESTEMAIL]", persistent.LDAP_Functions.GetMailAdress(request.Context.Session("user")))

            Dim sw As New IO.StreamWriter(request.OutputStream)
            If sw.BaseStream.CanSeek Then
                sw.BaseStream.Position = 0
            End If

            sw.WriteLine(sqlLoaderControlQuery)
            sw.WriteLine(srInputFile.ReadToEnd())
            sw.Close() : srControlTemplate.Close() : srInputFile.Close()

            request.InputStream.Close()
            request.OutputStream.Close()

            'AddHandler request.TelnetScript.ConnectionAttempFailure, AddressOf Me.OnConnectionAttempFailure
            'AddHandler request.TelnetScript.ScriptExecutionTimedout, AddressOf Me.OnScriptExecutionTimedout

            request.TelnetScript.Execute()

            Dim databaseResponse As DataTable = Me.callDatabaseWorker()

        End Sub

        Private Function getNextRequestID() As Int32
            Dim strSQL As String = "SELECT NVL(MAX(RequestID), -1) FROM EU_WRKND.TariffChangeRequests"
            Return CType(persistent.myDBConn.ORAScalar(strSQL), Int32) + 1
        End Function

        Private Function callDatabaseWorker() As DataTable
            Return New DataTable
            Dim strSQL As String = "EXEC EU_WRKND.[....]"
            Return persistent.myDBConn.myORATable(strSQL)
        End Function
    End Class


    Public Class MassiveTariffChangeRequest
        Private _Context As System.Web.HttpContext
        Private _InputStream As IO.Stream
        Private _OutputStream As IO.Stream
        Private _LocalWorkDirectory As String
        Private _RemoteWorkDirectory As String
        Private _TelnetScript As Telnet.Script

        Public Sub New(ByVal inputStream As IO.Stream, ByVal outputStream As IO.Stream, ByVal telnetHostAddress As String, ByVal context As System.Web.HttpContext)
            Me._InputStream = inputStream
            Me._OutputStream = outputStream
            Me._Context = context
            Me._LocalWorkDirectory = context.Server.MapPath("~/WorkDir/MassiveTariffChange")
            Me._TelnetScript = New Telnet.Script(telnetHostAddress, 23, 20000)
        End Sub

        Public ReadOnly Property Context() As System.Web.HttpContext
            Get
                Return Me._Context
            End Get
        End Property

        Public ReadOnly Property InputStream() As IO.Stream
            Get
                Return Me._InputStream
            End Get
        End Property

        Public ReadOnly Property OutputStream() As IO.Stream
            Get
                Return Me._OutputStream
            End Get
        End Property

        Public Property LocalWorkDirectory() As String
            Get
                Return Me._LocalWorkDirectory
            End Get
            Set(ByVal Value As String)
                Me._LocalWorkDirectory = Value
            End Set
        End Property

        Public ReadOnly Property TelnetScript() As Telnet.Script
            Get
                Return Me._TelnetScript
            End Get
        End Property
    End Class
End Namespace
